package com.dmall.play.interview.chapter07;

public class Leetcode235V1 {

    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if ((root.val - p.val) * (root.val - q.val) <= 0) {
            return root;
        }
        return lowestCommonAncestor(root.val > p.val ? root.left : root.right, p, q);
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        root.right = new TreeNode(2);
        Leetcode235V1 leetcode235 = new Leetcode235V1();
        TreeNode treeNode = leetcode235.lowestCommonAncestor(root, root, root.right);
        System.out.println(treeNode.val);
    }
}
